package com.example.controller;

import com.example.service.MultiThreadNotifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/multi-thread-notify")
public class MultiThreadNotifyController {

    @Autowired
    private MultiThreadNotifyService multiThreadNotifyService;

    @GetMapping("/wait-multi")
    public String waitForCondition() {
        for (int i = 1; i <= 3; i++) { // 启动三个线程
            int threadId = i;
            new Thread(() -> multiThreadNotifyService.waitForCondition(threadId)).start();
        }
        return "多个线程正在等待条件满足...";
    }

    @GetMapping("/notify-one")
    public String notifyCondition() {
        new Thread(() -> multiThreadNotifyService.notifyCondition()).start();
        return "已通知条件满足，唤醒一个线程...";
    }
}
